package com.rocksea.app.db.image;

import android.content.Context;
import android.database.Cursor;
import android.os.Environment;

import android.database.sqlite.SQLiteDatabase;
import com.litesuits.orm.LiteOrm;
import com.litesuits.orm.db.DataBaseConfig;
import com.litesuits.orm.db.assit.QueryBuilder;
import com.litesuits.orm.db.assit.SQLiteHelper;
import com.litesuits.orm.db.assit.WhereBuilder;
import com.litesuits.orm.db.model.ColumnsValue;
import com.litesuits.orm.db.model.ConflictAlgorithm;


import java.util.ArrayList;
import java.util.Date;

/**
 * Created by 吴志华 on 2017/10/17.
 *
 */

public class ImageDbHelper implements SQLiteHelper.OnUpdateListener
{
    private static LiteOrm imageDb;
    private static ImageDbHelper imageDbHelper;
    public ImageDbHelper(Context context)
    {
        DataBaseConfig config = new DataBaseConfig(context);
        config.dbName = Environment.getExternalStorageDirectory().getPath()+"/RailDatabase/db/ImageTable.db";
        config.dbVersion = 1;
        imageDb = LiteOrm.newSingleInstance(config);
        //imageDb.setDebugged(true);
        ImageTable imageTable = new ImageTable();
        imageDb.insert(imageTable);
        imageDb.delete(imageTable);
    }
    public static ImageDbHelper getInstance(Context context)
    {
        if (imageDbHelper==null)
        {
            synchronized (ImageDbHelper.class)
            {
                imageDbHelper = new ImageDbHelper(context);
            }
        }
        return imageDbHelper;
    }

    public void insert(ImageTable imageTable)
    {
        if (queryCount(imageTable.getSectionId()) > 0 && imageTable.getIsSonicWave() == 1)
        {
            //如果没上传
            if (!isUpload(imageTable.getSectionId()))
            {
                WhereBuilder whereBuilder = new WhereBuilder(ImageTable.class);
                whereBuilder.where("sectionId = ?",new Object[]{imageTable.getSectionId()});
                imageDb.delete(whereBuilder);
                imageDb.insert(imageTable);
            }
        }
        else
        {
            imageDb.insert(imageTable);
        }
    }

    public ArrayList<ImageTable> query(long planId)
    {
        WhereBuilder whereBuilder = new WhereBuilder(ImageTable.class);
        whereBuilder.andEquals("pileId",planId);
        QueryBuilder<ImageTable> queryBuilder = new QueryBuilder<>(ImageTable.class);
        queryBuilder.where(whereBuilder);
        return imageDb.query(queryBuilder);
    }
    public ArrayList<ImageTable> query(String planNum,String pileNo)
    {
        QueryBuilder<ImageTable> queryBuilder = new QueryBuilder<>(ImageTable.class);
        queryBuilder.whereEquals("planNum",planNum);
        queryBuilder.whereAppendAnd();
        queryBuilder.whereEquals("pileNo",pileNo);
        return imageDb.query(queryBuilder);
    }

    private boolean isUpload(String sectionId)
    {
        String sql = "select upLoadTime from ImageTable WHERE sectionId = ?";
        Cursor cursor = imageDb.getWritableDatabase().rawQuery(sql,new String[]{sectionId});
        if (cursor.moveToNext())
        {
            long value = cursor.getLong(cursor.getColumnIndex("upLoadTime"));
            cursor.close();
            return value > 0;
        }
        cursor.close();
        return false;
    }


    private long queryCount(String sectionId)
    {
        QueryBuilder<ImageTable> queryBuilder = new QueryBuilder<>(ImageTable.class);
        queryBuilder.whereEquals("isSonicWave",1);
        queryBuilder.whereAppendAnd();
        queryBuilder.whereEquals("sectionId",sectionId);
        return imageDb.queryCount(queryBuilder);
    }


    public void makeAsUpLoad(long planId,String fileName)
    {
        WhereBuilder whereBuilder = new WhereBuilder(ImageTable.class);
        whereBuilder.andEquals("pileId",planId);
        whereBuilder.andEquals("fileName",fileName);
        long upTime = new Date().getTime();
        ColumnsValue columnsValue = new ColumnsValue(new String[]{"upLoadTime"},new String[]{String.valueOf(upTime)});
        imageDb.update(whereBuilder,columnsValue,ConflictAlgorithm.Fail);
    }
    public void delete(long pileId,String fileName)
    {
        WhereBuilder whereBuilder = new WhereBuilder(ImageTable.class);
        whereBuilder.andEquals("pileId",pileId);
        whereBuilder.andEquals("fileName",fileName);
        imageDb.delete(whereBuilder);
    }

    public void queryAll()
    {
        imageDb.query(ImageTable.class);

    }

    public ArrayList<ImageTable> queryByPlanNum(String planNum)
    {
        WhereBuilder whereBuilder = new WhereBuilder(ImageTable.class);
        whereBuilder.andEquals("planNum",planNum);
        QueryBuilder<ImageTable> queryBuilder = new QueryBuilder<>(ImageTable.class);
        queryBuilder.where(whereBuilder);
        return imageDb.query(queryBuilder);
    }

    @Override
    public void onUpdate(SQLiteDatabase sqLiteDatabase, int i, int i1)
    {

    }
}
